﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;

using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;

namespace SZ
{
    class DocumentMapManageClass
    {
        // 新建
        public static void CreateMapDocument(AxMapControl mapControl)
        {
           try
            {
                DialogResult result = MessageBox.Show("是否保存当前地图文档?","",MessageBoxButtons.YesNo,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1 );
                if (result == DialogResult.Cancel) return;
                else if (result == DialogResult.Yes)
                {
                    //先保存地图文档
                    SaveAsMapDocument(mapControl);
                }
                string mxdFilePath = PublicVariablesClass.UntitleMapDocumentPath;
                if (mapControl.CheckMxFile(mxdFilePath))
                {
                    mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
                    mapControl.LoadMxFile(mxdFilePath, 0, Type.Missing);
                    mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
                    mapControl.Extent = mapControl.FullExtent;
                    mapControl.Refresh();
                }
                else
                {
                    MessageBox .Show ("不是有效的地图文档","",MessageBoxButtons.OK ,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
                }
            }
            catch
            {
                MessageBox.Show ("创建地图文档出错","",MessageBoxButtons.OK,MessageBoxIcon.Error ,MessageBoxDefaultButton .Button1 );
                return;
            }
        }

        //打开
        public static void OpenMapDocument(AxMapControl mapControl)
        {
            try
            {
                OpenFileDialog openMapDocument = new OpenFileDialog();
                openMapDocument.Title = "打开地图文档";
                openMapDocument.Filter = "地图文档(*.mxd)|*.mxd";
                openMapDocument.Multiselect = false;

                if (openMapDocument.ShowDialog() == DialogResult.OK)
                {
                    string mxdFilePath = openMapDocument.FileName;

                    if (mxdFilePath == mapControl.DocumentFilename)
                    {
                        MessageBox.Show("不能多次打开同一文档！");
                        return;
                    }

                    if (mapControl.CheckMxFile(mxdFilePath))
                    {
                        mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
                        mapControl.LoadMxFile(mxdFilePath);
                        mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
                        mapControl.Extent = mapControl.FullExtent;
                        mapControl.Refresh();
                    }
                }
            }
            catch
            {
                MessageBox.Show("打开地图文档失败！");
            }
        }

        //保存
        public static void SaveAsMapDocument(AxMapControl mapControl)
        {
            try
            {
                SaveFileDialog m_saveFileDialog = new SaveFileDialog();
                m_saveFileDialog.Title = "保存地图文档";
                m_saveFileDialog.Filter = "mxd文档|*.mxd";
                m_saveFileDialog.DefaultExt = ".mxd";
                m_saveFileDialog.AddExtension = true;
                m_saveFileDialog.FilterIndex = 0;

                if (m_saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    string saveMapPath = m_saveFileDialog.FileName;

                    IMapDocument mapDocument = new MapDocumentClass();
                    mapDocument.Open(mapControl.DocumentFilename, "");
                    IMxdContents mxdContents = mapControl.Map as IMxdContents;
                    mapDocument.ReplaceContents(mxdContents);
                    mapDocument.SaveAs(saveMapPath, true, true);
                }
            }
            catch
            {
                MessageBox.Show("保存地图文档失败！");
            }
        }
    }
}
